import tkinter as tk
from .edit_task import edit_task_func
from .delete_card import delete_card_func
from PIL import Image, ImageTk
from datetime import datetime
from tkinter import messagebox

def create_card_func(frame, task_name, priority, due_date, image_path):
    card_frame = tk.Frame(frame, relief=tk.RAISED, borderwidth=1, padx=10, pady=10)
    card_frame.pack(fill=tk.X, pady=5)
    
    task_label = tk.Label(card_frame, text=f"Task: {task_name}", anchor='w')
    task_label.pack(fill=tk.X)
    priority_label = tk.Label(card_frame, text=f"Priority: {priority}", anchor='w')
    priority_label.pack(fill=tk.X)
    due_date_label = tk.Label(card_frame, text=f"Due Date: {due_date}", anchor='w')
    due_date_label.pack(fill=tk.X)
    edit_button = tk.Button(card_frame, text="Edit", command=lambda: edit_task_func(card_frame, task_name, priority, due_date, image_path))
    edit_button.pack(anchor='s')
    delete_button = tk.Button(card_frame, text="Delete", command=lambda: delete_card_func(card_frame))
    delete_button.pack(anchor='s')
    created_time_label = tk.Label(card_frame, text=f"Created: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", anchor='w')
    created_time_label.pack(fill=tk.X)
    
    if image_path:
        try:
            img = Image.open(image_path)
            img = img.resize((100, 100))
            img = ImageTk.PhotoImage(img)
            image_label = tk.Label(card_frame, image=img)
            image_label.image = img
            image_label.pack()
        except Exception as e:
            messagebox.showerror("error", str(e))